﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IISLogAnalyzer
{
    public class Logger
    {
        private static readonly string LogFilePath;

        static Logger()
        {
            // 获取应用程序的根目录，并构建日志文件的完整路径  
            string appRootPath = AppDomain.CurrentDomain.BaseDirectory;
            LogFilePath = Path.Combine(appRootPath, "app.log");
        }

        public static void WriteLog(string message)
        {
            // 确保日志目录存在  
            string logDirectory = Path.GetDirectoryName(LogFilePath);
            if (!Directory.Exists(logDirectory))
            {
                Directory.CreateDirectory(logDirectory);
            }

            // 写入日志消息到文件  
            using (StreamWriter writer = File.AppendText(LogFilePath))
            {
                writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " - " + message);
                writer.Flush();
            }
        }
    }
}
